package main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.orm.SugarRecord;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import chat.network.tcp.PushService;
import main.network.Login;
import model.OwnUser;

/**
 * 登录界面
 * Created by 凯 on 2015/11/16.
 */
public class LoginActivity extends UIHandler.BaseActivity implements Login.Delegate {
    // 帐号
    EditText et_account;
    // 密码
    EditText et_password;
    // 登录按钮
    Button btn_login;
    // 滚动条
    View v_progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 检查用户是否已经登录
        checkUser();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initview();
    }

    // 检查用户是否已经登录
    private void checkUser() {

        OwnUser ownUser = MyApplication.getOwnUser();
        if (ownUser != null && ownUser.isLogined())
        {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
    View.OnClickListener itemclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_login) {
                login();
            }
        }
    };
    // 登录
    private void login() {
        String str_userName = et_account.getText().toString();
        String str_password = et_password.getText().toString();
        if (str_userName.isEmpty() || str_password.isEmpty()) {
            Toast.makeText(MyApplication.share(), "用户名、密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        v_progress.setVisibility(View.VISIBLE);
        UIHandler.uiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                v_progress.setVisibility(View.GONE);
            }
        }, 10000);
        new Login(this, str_userName, str_password).login();
    }

    // 初始化界面
    private void initview() {
        et_account = (EditText) findViewById(R.id.id_et_account);
        et_password = (EditText) findViewById(R.id.id_et_password);
        btn_login = (Button) findViewById(R.id.id_btn_login);
        btn_login.setOnClickListener(itemclick);
        v_progress = findViewById(R.id.id_logining);
        // 账号只能是字母与数字的组合
        et_account.setKeyListener(new NumberKeyListener() {
            @Override
            protected char[] getAcceptedChars() {
                StringBuffer sb = new StringBuffer();
                for (int i = 48; i <= 57; i++) {
                    sb.append((char) i);
                }

                for (int i = 65; i <= 90; i++) {
                    sb.append((char) i);
                }

                for (int i = 97; i <= 122; i++) {
                    sb.append((char) i);
                }

                return sb.toString().toCharArray();
            }

            @Override
            public int getInputType() {
                return InputType.TYPE_MASK_VARIATION;
            }
        });

    }

    @Override
    public void onResponse(String s, Login login) {
        if (login.getResponseSignIn() == null)return;
        if (login.getResponseSignIn().getStatus() == null) return;
        if (login.getResponseSignIn().getStatus().equals(Login.SignInStatus.SUCCESS))
        {
            v_progress.setVisibility(View.GONE);
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();

            Login.OwnUser responseUser = login.getResponseSignIn().getResponseUser();
            model.OwnUser ownUser = new model.OwnUser();
            ownUser.setUser_NickName(responseUser.getUser_NickName());
            ownUser.setUser_RealName(responseUser.getUser_RealName());
            ownUser.setIsLogined(true);
            ownUser.setUser_Gender(responseUser.isUser_Gender());
            ownUser.setUser_ID(responseUser.getUser_ID());
            ownUser.setUser_Role(responseUser.getUser_Role());
            SugarRecord.save(ownUser);

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();

            // 初始化推送服务
            Intent intent_push = new Intent(this,PushService.class);
            intent_push.putExtra(PushService.SERVICE_ACTION, PushService.ACTION_CONNECTION);
            startService(intent_push);

        }
        else
        {
            v_progress.setVisibility(View.GONE);
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onError(VolleyError volleyError, Login login) {
        Toast.makeText(this,"服务器错误",Toast.LENGTH_SHORT).show();
    }
}
